# 微信好友信息获取，并生成csv文件
import itchat
from pandas import DataFrame


def lc():
    print("Login!")


def ec():
    print("exit!")


def get_var(var):
    variable = []
    for j in friends[1:]:
        value = j[var]
        variable.append(value)
    return variable


male = female = other = 0

itchat.auto_login(loginCallback=lc, exitCallback=ec, hotReload=True)
friends = itchat.get_friends(update=True)[0:]  # update=True更新好友列表并返回

for i in friends[1:]:
    sex = i["Sex"]
    if sex == 1:
        male += 1
    elif sex == 2:
        female += 1
    else:
        other += 1

total = len(friends[1:])
print("total=", total)
print("男性好友： %.2f%%" % (float(male) / total * 100))
print("女性好友： %.2f%%" % (float(female) / total * 100))
print("不明性别好友： %.2f%%" % (float(other) / total * 100))

NickName = get_var("NickName")
Sex = get_var("Sex")
Province = get_var("Province")
City = get_var("City")
Signature = get_var("Signature")

data = {'NickName': NickName, 'Sex': Sex, 'Province': Province,
        'City': City, 'Signature': Signature}
frame = DataFrame(data)
frame.to_csv('data.csv', index=True)

itchat.logout()  # 强制退出登录
